package com.onlyxiahui.extend.query.hibernate.syntax.util;

import java.util.Map;
import java.util.Set;

import com.onlyxiahui.extend.query.hibernate.QueryWrapper;

/**
 * 
 * Description <br>
 * Date 2020-02-01 11:33:02<br>
 * 
 * @author XiaHui [onlovexiahui@qq.com]<br>
 * @since 1.0.0
 */
public class UpdateSqlUtil {

	public static String getUpdateSql(String tableName, String idName, QueryWrapper qw) {

		StringBuilder sql = new StringBuilder();
		Map<String, Object> map = (null == qw) ? null : qw.getParameterMap();
		if (map != null && !map.isEmpty()) {
			Set<String> keySet = map.keySet();
			sql.append(" update ");
			sql.append(tableName);

			sql.append(" set ");
			int i = 0;
			// int size=keySet.size();
			for (String key : keySet) {
				if (!idName.equals(key)) {
					i++;
					if (i == 1) {
						sql.append(key);
						sql.append(" = ");
						sql.append(":");
						sql.append(key);
					} else {
						sql.append(",");
						sql.append(key);
						sql.append(" = ");
						sql.append(":");
						sql.append(key);
					}
				}
			}

			sql.append(" where ");
			sql.append(idName);
			sql.append(" = ");
			sql.append(":");
			sql.append(idName);
		}
		return sql.toString();
	}
}
